﻿using BingVis.GoogleTransit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BingVis.Common;
using System.Threading;
using System;

namespace BingVis.GoogleTransit.Test
{
    
    
    /// <summary>
    ///This is a test class for FareTapLoaderTest and is intended
    ///to contain all FareTapLoaderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class FareTapLoaderTest
    {

        protected string[] testFareContents = new string[] 
        {
            "time, stop_name, card_type, route_name, amount",
            @"""08/12/2009 07:07:24"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 07:40:28"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:05:15"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:09:11"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:11:02"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:14:35"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"", ",
            @"""08/12/2009 08:14:35"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:15:25"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:16:00"",""Newton Centre"",""Large Triplex Roll mag. Stripe"",""Subway Green Line"",1.00",
            @"""08/12/2009 08:28:30"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
        };

        /// <summary>
        ///A test for ProcessItem
        ///</summary>
        [TestMethod()]
        public void Should_Process_10_Lines_From_TestFareContents()
        {
            IGTFSFile file = new GTFSFile("blah.txt", testFareContents);
            FareTapLoader target = new FareTapLoader(file);
            int processedCount = 0;
            bool done = false;
            target.ItemLoaded += (x, y) => processedCount++;
            target.DoneProcessing += (x,y) => done = true;

            Assert.AreNotEqual(10, target.Items.Count);

            target.ProcessFile();
            while (!done)
            {
                Thread.Sleep(200);
            }
            
            Assert.AreEqual<int>(10, processedCount);
            Assert.AreEqual<int>(10, target.Items.Count);
        }

        /// <summary>
        ///A test for ProcessItem
        ///</summary>
        [TestMethod()]
        public void Should_Process_Items_Into_FareTaps()
        {
            IGTFSFile file = new GTFSFile("blah.txt", testFareContents);
            FareTapLoader loader = new FareTapLoader(file);
            int processedCount = 0;
            bool done = false;
            loader.ItemLoaded += (x, y) => processedCount++;
            loader.DoneProcessing += (x, y) => done = true;

            Assert.AreNotEqual(10, loader.Items.Count);

            loader.ProcessFile();
            while (!done)
            {
                Thread.Sleep(200);
            }

            var target = loader.Items[0];

            // Verify Values from testFareContents.
            //"time, stop_name, card_type, route_name, amount",
            //@"""08/12/2009 07:07:24"",""Newton Centre"",""Smart Card Mifare 1k"",""Subway Green Line"",1.00",
            Assert.AreEqual<DateTime>(DateTime.Parse("08/12/2009 07:07:24"), target.Time);
            Assert.AreEqual<string>("Newton Centre", target.StopName);
            Assert.AreEqual<string>("Smart Card Mifare 1k", target.CardType);
            Assert.AreEqual<string>("Subway Green Line", target.RouteName);
            Assert.AreEqual<decimal>(1.00M, target.Amount);
        }

        /// <summary>
        ///A test for FareTapLoader Constructor
        ///</summary>
        [TestMethod()]
        public void Should_Load_Members_Properly()
        {
            IGTFSFile file = new GTFSFile("blah.txt", testFareContents);
            FareTapLoader target = new FareTapLoader(file);

            Assert.IsNotNull(target.Items);
        }
   }
}
